import {message} from 'antd'
import {queryAdvert, removeAdvert, addAdvert, editAdvert, queryAdvertItem} from '../services/api';


export default {
  namespace: 'advert',

  state: {
      data: {list: [], pagination: {size: 10, current: 1, total: 0}},
      item:{},
      options: [
          {value:'index_left',label:'首页左侧广告'},
          {value:'index_right_1',label:'首页右侧广告1'},
          {value:'index_right_2',label:'首页右侧广告2'},
          {value:'index_section',label:'首页下侧广告'},
          {value:'category_top',label:'分类顶部广告'}
      ],
      pages: [
          {title:"首页",link:"/pages/index/index"} ,
          {title:'服务',link:'/pages/service/service'},
          {title:'登录',link:'pages/user/login/login'},
          {title:'服务预约',link:'/pages/service/upload/upload'},
          {title:'师傅入住',link:'/pages/user/update/update'},
          {title:'商城首页',link:'/pages/shop/shop'},
          {title:'服务详情',link:'/pages/service/detail/detail'},
          {title:'商品详情',link:'/pages/shop/detail/detail'},
          {title:'外部链接',link:'out'}
      ],
      cashModal: false,
      loading: false,
      confirmLoading:false,
  },

  effects: {
    *fetch({payload}, { call, put }) {
        yield put({
            type: 'changeLoading',
            payload: true,
        });
        const response = yield call(queryAdvert, payload);
        yield put({
            type: 'save',
            payload: {pagination: response.page, list: response.data},
        });
        yield put({
            type: 'changeLoading',
            payload: false,
        });
    },
      *fetchItem({payload,callback}, { call, put }) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(queryAdvertItem, payload);
          yield put({
              type: 'saveItem',
              payload: response.data,
          });
          if(response.status && callback){
             callback()
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *remove({payload,callback}, { call, put }) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(removeAdvert, payload);
          if(response.status){
              message.success('删除成功');
              if(callback){
                callback();
              }
          }else {
              message.success(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *add({payload,callback}, { call, put }) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(addAdvert, payload);
          if(response.status){
              message.success('添加成功');
              if(callback){
                  callback();
              }
          }else {
              message.success(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *edit({payload,callback}, { call, put }) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(editAdvert, payload);
          if(response.status){
              message.success('编辑成功');
              if(callback){
                  callback();
              }
          }else {
              message.success(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
    // *fetchSalesData(_, { call, put }) {
    //   const response = yield call(fakeChartData);
    //   yield put({
    //     type: 'save',
    //     payload: {
    //       salesData: response.salesData,
    //     },
    //   });
    // },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        data:payload,
      };
    },
      saveItem(state, { payload }) {
          return {
              ...state,
              item:payload,
          };
      },
    changeLoading(state, { payload }) {
      return {
        ...state,
        loading:payload,
      };
    },
    clear() {
      return {
        visitData: [],
        visitData2: [],
        salesData: [],
        searchData: [],
        offlineData: [],
        offlineChartData: [],
        salesTypeData: [],
        salesTypeDataOnline: [],
        salesTypeDataOffline: [],
        radarData: [],
      };
    },
  },
};
